package cn.com.thingo.shyintelligent.component;

import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;

import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.app.App;

/**
 * Created by Administrator on 2017/10/9.
 */

public class ImageLoader {

   /* RequestOptions requestOptions = new RequestOptions()
            .placeholder(R.mipmap.placeholder) //加载中图片
            .error(R.mipmap.error) //加载失败图片
            .fallback(R.mipmap.fallback) //url为空图片
            .centerCrop() // 填充方式
            .diskCacheStrategy(DiskCacheStrategy.NONE); //缓存策略，后面详细介绍*/

    public static void load(Context context, String url, ImageView iv) {    //使用Glide加载圆形ImageView(如头像)时，不要使用占位图
        if (!App.getAppComponent().preferencesHelper().getNoImageState()) {
            Glide.with(context).load(url).apply(new RequestOptions().error(R.drawable.svg_logo).placeholder(R.drawable.svg_logo).diskCacheStrategy(DiskCacheStrategy.ALL)).transition(new DrawableTransitionOptions().crossFade(500)).into(iv);
        }
    }

    public static void load(Activity activity, String url, ImageView iv) {    //使用Glide加载圆形ImageView(如头像)时，不要使用占位图
        if(!activity.isDestroyed()) {
            Glide.with(activity).load(url).apply(new RequestOptions().error(R.drawable.svg_logo).placeholder(R.drawable.svg_logo).diskCacheStrategy(DiskCacheStrategy.ALL)).transition(new DrawableTransitionOptions().crossFade(500)).into(iv);
        }
    }

    public static void loadAll(Context context, String url, ImageView iv) {    //不缓存，全部从网络加载
        Glide.with(context).load(url).apply(new RequestOptions().error(R.drawable.svg_logo).placeholder(R.drawable.svg_logo).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)).transition(new DrawableTransitionOptions().crossFade(500)).into(iv);
    }

    public static void loadAll(Activity activity, String url, ImageView iv) {    //不缓存，全部从网络加载
        if(!activity.isDestroyed()) {
            Glide.with(activity).load(url).apply(new RequestOptions().error(R.drawable.svg_logo).placeholder(R.drawable.svg_logo).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)).transition(new DrawableTransitionOptions().crossFade(500)).into(iv);
        }
    }
}
